<template>
  <div class="app-container">
    <el-card class="box-card" shadow="never">
      <!-- 数据展示 -->
      <el-table
        :data="tableData"
        :header-cell-style="{background:'#f0f3fa',textAlign:'center'}"
        style="width: 100%"
        :row-class-name="tableRowClassName"
        :cell-style="{textAlign:'center'}"
      >
        <el-table-column prop="nameZh" label="账号类型"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope" v-if="scope.row.id !== 1">
            <el-switch
              v-model="scope.row.enabled"
              :active-value="1"
              :inactive-value="0"
              @change="handleChangeSwitch(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope" v-if="scope.row.id !== 1">
            <el-tooltip class="item" content="权限设置">
              <i class="el-icon-key" @click="handleEdit(scope.row)"></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="dataForm.page"
        :page-sizes="[10, 20, 40, 60]"
        :page-size="dataForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 密码修改 -->
    <jurisdiction ref="jurisdiction" @updata="getRoleEnabledInit"></jurisdiction>
  </div>
</template>

<script>
import jurisdiction from './component/jurisdiction.vue'
import { getRoleEnabled, updateRoleEnabled } from '@/api/other'
export default {
  // 组件注册
  components: {
    jurisdiction,
  },
  data() {
    return {
      dataForm: {
        page: 1,
        pageSize: 10,
      },
      tableData: [],
      total: 0,
    }
  },
  mounted() {
    this.getRoleEnabledInit()
  },
  methods: {
    getRoleEnabledInit() {
      getRoleEnabled(this.dataForm).then((res) => {
        const { code, data, msg } = res
        if (code !== '0000') return this.$message.error(msg)
        const { total, records } = data
        this.total = total
        this.tableData = records
      })
    },
    handleChangeSwitch({ id, enabled }) {
      updateRoleEnabled({ id, enabled }).then((res) => {
        const { code, msg } = res
        if (code !== '0000') return this.$message.error(msg)
        this.getRoleEnabledInit()
      })
    },
    handleEdit(row) {
      this.$nextTick((_) => {
        this.$refs.jurisdiction.init(row)
      })
    },
    handleDelete(index, row) {
      console.log('删除', index, row)
    },
    // 监听pageSize改变事件
    handleSizeChange(newSize) {
      this.dataForm.pageSize = newSize
      this.getRoleEnabledInit()
    },
    // 监听 页面值 改变的事件
    handleCurrentChange(newPage) {
      this.dataForm.page = newPage
      this.getRoleEnabledInit()
    },
  },
}
</script>

<style lang='scss' scoped>
</style>
